//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "PiecewiseConstantParameter.h"
using namespace Cephei::QL::Models;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Parameter.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::CPiecewiseConstantParameter::CPiecewiseConstantParameter (Cephei::IVector<Double>^ times) : CParameter(CPiecewiseConstantParameter::typeid)
{
    CoVector<Double>^ _Ctimes;
    try
    {
#ifdef HANDLE
        _phPiecewiseConstantParameter = NULL;
#endif
        CoVector<Double>^ _Ctimes = safe_cast<CoVector<Double>^> (times);
        _Ctimes->Lock();
        INativeVector<Double>^ _NCItimes = _Ctimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCtimes = safe_cast<CDoubleVector^>(_NCItimes);
        std::vector<QuantLib::Time>& _times = static_cast<std::vector<QuantLib::Time>&> (_NCtimes->GetReference ());
        _ppPiecewiseConstantParameter = new boost::shared_ptr<QuantLib::PiecewiseConstantParameter> (new QuantLib::PiecewiseConstantParameter ( _times ));
        SetParameter (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppPiecewiseConstantParameter));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctimes != nullptr) _Ctimes->Unlock();
    }
}
Cephei::QL::Models::CPiecewiseConstantParameter::CPiecewiseConstantParameter (boost::shared_ptr<QuantLib::PiecewiseConstantParameter>& childNative, Object^ owner) : CParameter(CPiecewiseConstantParameter::typeid)
{
#ifdef HANDLE
	_phPiecewiseConstantParameter = NULL;
#endif
	_ppPiecewiseConstantParameter = &childNative;
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppPiecewiseConstantParameter));
}
Cephei::QL::Models::CPiecewiseConstantParameter::CPiecewiseConstantParameter (QuantLib::PiecewiseConstantParameter& childNative, Object^ owner) : CParameter(CPiecewiseConstantParameter::typeid)
{
#ifdef HANDLE
	_phPiecewiseConstantParameter = NULL;
#endif
	_ppPiecewiseConstantParameter = new boost::shared_ptr<QuantLib::PiecewiseConstantParameter> (&childNative);
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppPiecewiseConstantParameter));
    _PiecewiseConstantParameterOwner = owner;
    _ParameterOwner = owner;
}

Cephei::QL::Models::CPiecewiseConstantParameter::CPiecewiseConstantParameter (CPiecewiseConstantParameter^ copy) : CParameter(CPiecewiseConstantParameter::typeid)
{
#ifdef HANDLE
	_phPiecewiseConstantParameter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPiecewiseConstantParameter = new boost::shared_ptr<QuantLib::PiecewiseConstantParameter> (copy->GetShared());
        _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppPiecewiseConstantParameter));
    }
}
Cephei::QL::Models::CPiecewiseConstantParameter::CPiecewiseConstantParameter (System::Type^ t) : CParameter(CPiecewiseConstantParameter::typeid)
{
#ifdef HANDLE
	_phPiecewiseConstantParameter = NULL;
#endif
	if (!t->IsSubclassOf(CPiecewiseConstantParameter::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::CPiecewiseConstantParameter::CPiecewiseConstantParameter (QuantLib::Handle<QuantLib::PiecewiseConstantParameter>& childNative, Object^ owner)  : CParameter(CPiecewiseConstantParameter::typeid)
{
	_phPiecewiseConstantParameter = &childNative;
	_ppPiecewiseConstantParameter = &static_cast<boost::shared_ptr<QuantLib::PiecewiseConstantParameter>>(childNative.currentLink());
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppPiecewiseConstantParameter));
    _PiecewiseConstantParameterOwner = owner;
}
Cephei::QL::Models::CPiecewiseConstantParameter::CPiecewiseConstantParameter (QuantLib::Handle<QuantLib::PiecewiseConstantParameter> childNative)  : CParameter(CPiecewiseConstantParameter::typeid)
{
	_phPiecewiseConstantParameter = &childNative;
	_ppPiecewiseConstantParameter = &static_cast<boost::shared_ptr<QuantLib::PiecewiseConstantParameter>>(childNative.currentLink());
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppPiecewiseConstantParameter));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::CPiecewiseConstantParameter::CPiecewiseConstantParameter (QuantLib::PiecewiseConstantParameter childNative)  : CParameter(CPiecewiseConstantParameter::typeid)
{
#ifdef HANDLE
	_phPiecewiseConstantParameter = NULL;
#endif
	_ppPiecewiseConstantParameter = new boost::shared_ptr<QuantLib::PiecewiseConstantParameter> (new QuantLib::PiecewiseConstantParameter (childNative));
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppPiecewiseConstantParameter));
}
#endif

Cephei::QL::Models::CPiecewiseConstantParameter::~CPiecewiseConstantParameter ()
{
    if (_ppPiecewiseConstantParameter != NULL)
    {
	    delete _ppPiecewiseConstantParameter;
        _ppPiecewiseConstantParameter = NULL;
    }
}
Cephei::QL::Models::CPiecewiseConstantParameter::!CPiecewiseConstantParameter ()
{
    if (_ppPiecewiseConstantParameter != NULL)
    {
	    delete _ppPiecewiseConstantParameter;
    }
}
QuantLib::PiecewiseConstantParameter& Cephei::QL::Models::CPiecewiseConstantParameter::GetReference ()
{
    if (_ppPiecewiseConstantParameter == NULL) throw gcnew NativeNullException ();
	return **_ppPiecewiseConstantParameter;
}
boost::shared_ptr<QuantLib::PiecewiseConstantParameter>& Cephei::QL::Models::CPiecewiseConstantParameter::GetShared ()
{
    if (_ppPiecewiseConstantParameter == NULL) throw gcnew NativeNullException ();
	return *_ppPiecewiseConstantParameter;
}
QuantLib::PiecewiseConstantParameter* Cephei::QL::Models::CPiecewiseConstantParameter::GetPointer ()
{
    if (_ppPiecewiseConstantParameter == NULL) throw gcnew NativeNullException ();
	return &**_ppPiecewiseConstantParameter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PiecewiseConstantParameter>& Cephei::QL::Models::CPiecewiseConstantParameter::GetHandle ()
{
	if (_phPiecewiseConstantParameter == NULL)
	{
		_phPiecewiseConstantParameter = new Handle<QuantLib::PiecewiseConstantParameter> (*_ppPiecewiseConstantParameter);
	}
	return *_phPiecewiseConstantParameter;
}
#endif
bool Cephei::QL::Models::CPiecewiseConstantParameter::HasNative () 
{
	return (_ppPiecewiseConstantParameter != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::IPiecewiseConstantParameter^ Cephei::QL::Models::CPiecewiseConstantParameter_Factory::Create (Cephei::IVector<Double>^ times)
{
    return gcnew CPiecewiseConstantParameter ( times);
}
